package com.gitee.wsl.common.chart.render.foreground.info

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import com.gitee.wsl.common.chart.ChartEnum
import com.gitee.wsl.common.chart.render.foreground.border.BorderDrawableReader
import com.gitee.wsl.common.ui.draw.type.RectType
import com.gitee.wsl.android.ui.ext.type.drawRotateText
import com.gitee.wsl.android.ui.ext.type.textHeight
import com.gitee.wsl.android.ui.ext.type.textWidth
import com.gitee.wsl.mathematics.geometry.shape.Rectangle

class PlotLabel: Rectangle() {

    //标签与框的保留间距
    var margin = 5f

    //box
    val borderReader: BorderDrawableReader by lazy {
        BorderDrawableReader(this).apply {
            rectType = RectType.RECT
            backgroundColor = Color.rgb(240, 255, 112)
            backgroundAlpha = DEF_BOX_BG_ALPHA
        }
    }

    var borderColor :Int by borderReader::borderColor

    //protected boolean mShowBox = false;
    var isShowBoxBorder by borderReader::isShowBorder
    var isShowBackground by borderReader::isShowBackground

    //protected boolean mShowBoxCap = true;

    //protected boolean mShowBoxCap = true;
    //标签起始偏移多少距离
    var offsetX = 0.0f
    var offsetY = 0.0f

    //带箭头的框中，箭头的高度
    var scale = 0.2f

    //圆框半径
    override var radius = 0f

    var labelBoxStyle = ChartEnum.LabelBoxStyle.CAPRECT
        set(value) {
        field=value
        when(value){
            ChartEnum.LabelBoxStyle.TEXT->{
                isShowBoxBorder = false
                isShowBackground = false
            }
            ChartEnum.LabelBoxStyle.CIRCLE->{
                isShowBoxBorder = false
                isShowBackground = true
            }
            else->{
                isShowBoxBorder = true
                isShowBackground = true
            }
        }
    }

    var rectBox: RectF = RectF()

    fun renderLabel(
        canvas: Canvas, paint: Paint, label: String,
        cX: Float, cY: Float, itemAngle: Float, borderColor: Int
    ): Boolean {
        this.borderColor = borderColor
        return renderLabel(canvas, paint, label, cX, cY, itemAngle)
    }

    fun renderLabel(
        canvas: Canvas, paint: Paint, label: String,
        cX: Float, cY: Float, itemAngle: Float
    ): Boolean {
        if ("" === label || label.isEmpty()) return false
        //if (null == canvas || null == paint) return false
        var left = 0.0f
        var top = 0.0f
        var right = 0.0f
        var bottom = 0.0f
        val w: Float = paint.textWidth(label)
        val h: Float = paint.textHeight
        val x = cX + offsetX
        var y = cY - offsetY
        if (ChartEnum.LabelBoxStyle.TEXT == labelBoxStyle) {
            canvas.drawRotateText(label, x, y - margin, itemAngle, paint)
            return true
        }
        if (ChartEnum.LabelBoxStyle.CIRCLE == labelBoxStyle) {
            var radius = radius
            if (this.radius.compareTo(0f) == 0 || this.radius.compareTo(0f) == -1) {
                try {
                    radius = w.coerceAtLeast(h)
                    radius = radius / 2 + 5f
                } catch (ex: Exception) {
                    radius = 25f
                }
            }
            y = y - margin - radius
            canvas.drawCircle(x, y, radius, borderReader.backgroundPaint)
            if (isShowBoxBorder) canvas.drawCircle(x, y, radius, borderReader.linePaint)
            canvas.drawRotateText(label, x, y, itemAngle,  paint)
            return true
        } else {
            left = x - w / 2 - margin
            right = x + w / 2 + margin
            top = y - h - margin
            bottom = y
            rectBox.left = left
            rectBox.right = right
            rectBox.top = top
            rectBox.bottom = bottom
            if (ChartEnum.LabelBoxStyle.RECT == labelBoxStyle) {
                drawBox(canvas)
                canvas.drawRotateText(label, x, y - margin, itemAngle,  paint)
            } else {
                val AngleH: Float = rectBox.width() * scale //0.2f ;
                rectBox.top -= AngleH
                rectBox.bottom -= AngleH
                if (borderColor != -1) borderReader.linePaint.color=borderColor
                when (labelBoxStyle) {
                    ChartEnum.LabelBoxStyle.CAPRECT ->
                        borderReader.renderCapRect(
                        canvas,
                        rectBox,
                        AngleH,
                        isShowBoxBorder,
                        isShowBackground
                    )
                    ChartEnum.LabelBoxStyle.CAPROUNDRECT ->
                        borderReader.renderCapRound(
                        canvas,
                        rectBox,
                        AngleH,
                        isShowBoxBorder,
                        isShowBackground
                    )
                    ChartEnum.LabelBoxStyle.ROUNDRECT ->
                        borderReader.renderRound(
                        canvas,
                        rectBox,
                        AngleH,
                        isShowBoxBorder,
                        isShowBackground
                    )
                    else -> {}
                }
                canvas.drawRotateText(label, x, y - margin - AngleH, itemAngle,  paint)
            }
            rectBox.setEmpty()
        }
        return true
    }

    private fun drawBox(canvas: Canvas) {
        if (borderColor != -1) borderReader.linePaint.color = borderColor
        borderReader.renderRect(canvas, rectBox, isShowBoxBorder, isShowBackground)
    }
    
    companion object{
        const val DEF_BOX_BG_ALPHA = 100
    }
}